package iscript.gameFramework.GameLite.content 
{
	import com.greensock.events.LoaderEvent;
	import com.greensock.loading.core.LoaderCore;
	import com.greensock.loading.data.ImageLoaderVars;
	import com.greensock.loading.data.LoaderMaxVars;
	import com.greensock.loading.ImageLoader;
	import com.greensock.loading.LoaderMax;
	import com.greensock.loading.LoaderStatus;
	import flash.utils.Dictionary;
	import iscript.gameFramework.GameLite.events.GameContentEvent;

	internal class ImageContent implements IGameContent
	{
		
		private var _data:Dictionary = new Dictionary();	
		private var _loaderMax:LoaderMax;
		private var _gameContent:GameContent;
		
		
		public function ImageContent(gameContent:GameContent) 
		{
			   _gameContent = gameContent
			   var loadvars:LoaderMaxVars = new LoaderMaxVars();
				   loadvars.autoLoad(true);
				   loadvars.maxConnections(3);
				   loadvars.onChildComplete(_onChildCompleteLogic)
				   _loaderMax = new LoaderMax(loadvars);
				   _loaderMax.load();
		}
		
		private function _onChildCompleteLogic(e:LoaderEvent):void 
		{
			 _dispatch(ImageLoader(e.target));
		}
		
		private function _dispatch(load:ImageLoader):void {
			   var imageload:ImageLoader = load;
			   var event:GameContentEvent = new GameContentEvent(GameContentEvent.ONLOADIMAGE);
			       event.content =  imageload;
				   event.key =      imageload.vars[GameContent.GamneContentKey];
			       _gameContent.dispatchEvent(event);
				   
				 var ev:GameContentEvent = new GameContentEvent(GameContentEvent.EVERYTHING)
				     ev.key = imageload.vars[GameContent.GamneContentKey];
					 ev.content = imageload;
				   _gameContent.dispatchEvent(ev);
		}
		
		
		//public function get KEY():Array{return _data.getAllKeys()}	
		
		/**
		 * often time key==url   经常碰到把URL作为KEY，所以用的时候key和url经常是一个值.
		 * @param	key
		 * @param	url
		 * @param	param
		 */
		public function add(url:String, key:*, param:ImageLoaderVars = null):void {	  
			if(_data[key]==null) { 
			  if (param == null)  param = new ImageLoaderVars();
					param.vars[GameContent.GamneContentKey] = key;
			   var  imageload:ImageLoader = new ImageLoader(url, param);
			        _data[key] = imageload
			       _loaderMax.append(imageload);
				}
		}
		 
        /**
         * imageloader 必须是加载完成的
         * @param	key
         * @param	imageLoad
         */
		public function addImageLoader(key:*, imageLoader:ImageLoader):void {
			 if (imageLoader.status == LoaderStatus.COMPLETED) {
				if(_data[key]==null){
					_data[key] = imageLoader;
					_dispatch(imageLoader);
				    }
				 }
		}  
		 
		public function get(key:*):ImageLoader
		{
			return _data[key]
		}
		
		public function contain(key:*):Boolean {
			  return  _data[key] != null;
		}
		
		public function remove(key:*):void
		{
			if (_data[key] != null) {
			    LoaderCore(_data[key]).unload();
			    LoaderCore(_data[key]).dispose();
				delete _data[key];		
			}
		}
		
	}

}